﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics;

namespace Kosmos
{
    class LevelScreen : GameScreen
    {
        #region Fields

        ContentManager content;
        Level level;

        private float _scroll;
        //private float _scroll_value = 0.5f;

        CameraEffect _camera;
        PhysicsSimulator _simulator;
        Player _player;
        SpriteBatch _spriteBatch;


        #endregion

        #region Getters and Setters

        public PhysicsSimulator Simulator
        {
            get { return _simulator; }
            set { _simulator = value; }
        }

        public CameraEffect Camera
        {
            get { return _camera; }
            set { _camera = value; }
        }

        public Player Player
        {
            get { return _player; }
        }

        public float Scroll
        {
            get { return _scroll; }
            set { _scroll = value; }
        }

        public SpriteBatch SpriteBatch
        {
            get { return _spriteBatch; }
            set { _spriteBatch = value; }
        }

        #endregion

        #region Initialization

        public LevelScreen(Level level, CameraEffect camera)
        {
            this.level = level;

            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            Scroll = Mouse.GetState().ScrollWheelValue;
            SpriteBatch = new SpriteBatch(Program.game.GraphicsDevice);

            Simulator = new PhysicsSimulator(new Vector2(0, 0));

            Camera = camera;
            Camera.InitEffect(new EffectProgressiveZoom(5000, 10f));

            Program.game.Components.Add(level);

            _player = new Player(Program.game, level.PlayerBall);
        }

        public override void LoadContent()
        {
            
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

        }

        #endregion

        #region Handle Input


        #endregion

        #region Draw

        protected void GestionCamera(GameTime gameTime)
        {
            if (_scroll != Mouse.GetState().ScrollWheelValue)
            {
                float tmp = ((_scroll <= Mouse.GetState().ScrollWheelValue) ? (_camera.Zoom) * 1.2f : (_camera.Zoom) / 1.1f);
                //(_scroll <= _mouse.ScrollWheelValue) ? _scroll_value : -_scroll_value;
                //_camera.Zoom += tmp;            
                _camera.InitEffect(new EffectProgressiveZoom(500, tmp));
                _scroll = Mouse.GetState().ScrollWheelValue;

            }

            Camera.Update(gameTime);


            if (_camera.Zoom > 300 / level.PlayerBall.diameter)
                _camera.Zoom = 300 / level.PlayerBall.diameter;

            _camera.Focus = level.PlayerBall.center * _camera.Zoom;
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

            _simulator.Update(gameTime.ElapsedGameTime.Milliseconds * 0.001f);

            GestionCamera(gameTime);
        
        }

        public override void Draw(GameTime gameTime)
        {
            Program.game.GraphicsDeviceManager.GraphicsDevice.Clear(new Color(0, 0, 0));

            _spriteBatch.Begin(
                SpriteBlendMode.AlphaBlend,
                SpriteSortMode.Immediate,
                SaveStateMode.SaveState,
                Camera.getTransformation(_spriteBatch.GraphicsDevice)
            );
            base.Draw(gameTime);
            _spriteBatch.End();
        }

        #endregion

    }
}
